package com.kaluli.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.oxm.xstream.XStreamMarshaller;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.kaluli.system.interceptor.UserInterceptor;
import com.thoughtworks.xstream.converters.basic.DateConverter;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.kaluli.project.controller",
		"com.kaluli.system.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/res/**").addResourceLocations("/res/");
	}

	@Override
	public void configureMessageConverters(
			List<HttpMessageConverter<?>> converters) {
		converters.add(xmlConverter());
		converters.add(byteArrayConverter());
		converters.add(jsonConverter());
		super.configureMessageConverters(converters);
	}
	
	private ByteArrayHttpMessageConverter byteArrayConverter(){
		ByteArrayHttpMessageConverter byteArrayConverter = new ByteArrayHttpMessageConverter();
		byteArrayConverter
				.setSupportedMediaTypes(Arrays.asList(MediaType.IMAGE_GIF,
						MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG , MediaType.ALL));
		return byteArrayConverter;
	}

	private MappingJacksonHttpMessageConverter jsonConverter(){
		MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
		jsonConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON));
		return jsonConverter;
	}
	
	private MarshallingHttpMessageConverter xmlConverter() {
		MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
		XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
		HashMap	<String, Class> aliases = new HashMap<String, Class>();
//		aliases.put("data", MeasuredData.class);
		aliases.put("datas", ArrayList.class);
//		aliases.put("alarmdata", AlarmData.class);
		aliases.put("plans", List.class);
//		aliases.put("system", SystemInfo.class);
/*		aliases.put("point", Point.class);
		aliases.put("item", Item.class);
		aliases.put("position", Position.class);*/

		xstreamMarshaller.setAliases(aliases);

		DateConverter converter = new DateConverter("yyyy-MM-dd", new String[] {
				"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd G HH:mm:ss.S z" });
		xstreamMarshaller.getXStream().registerConverter(converter);
		xmlConverter.setMarshaller(xstreamMarshaller);
		xmlConverter.setUnmarshaller(xstreamMarshaller);
		return xmlConverter;
	}

	@Bean
	public ViewResolver viewResolver() {
		InternalResourceViewResolver view = new InternalResourceViewResolver();
		view.setPrefix("/views/");
		view.setSuffix(".jsp");
		view.setCache(false);
		return view;
	}

	// @Bean
	// public ViewResolver xmlViewResolver() {
	// ContentNegotiatingViewResolver view = new ContentNegotiatingViewResolver(
	// );
	// // view.setMediaTypes(mediaTypes)
	//
	// view.setMediaTypes(null);
	// view.setDefaultViews(defaultViews)
	//
	// return view;
	// }

	@Bean
	public MultipartResolver multipartResolver() {
		CommonsMultipartResolver mutipartResolver = new CommonsMultipartResolver();
		mutipartResolver.setDefaultEncoding("utf-8");
		// mutipartResolver.setMaxInMemorySize(100 * 1024);
		mutipartResolver.setMaxUploadSize(20 * 1048576);
		return mutipartResolver;
	}
	
	@Bean
	public UserInterceptor userInterceptor(){
		return new UserInterceptor();
	}
	
	 @Override
	 public void addInterceptors(InterceptorRegistry registry) {
	
//		 LocaleChangeInterceptor localeChangeInterceptor = new
//		 LocaleChangeInterceptor();
//		 localeChangeInterceptor.setParamName("lang");
//		 registry.addInterceptor(localeChangeInterceptor);
		 
		 registry.addInterceptor(userInterceptor());
	 }
	//
}
